package view.view2D;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import model.Coordinate;
import model.GameStatus;
import model.Ship;
import model.games.SinglePlayer;
import model.mobs.Mob;
import view.ScoreDrawer;
import view.ViewParam;
import control.Controller2D;

public class SinglePlayer2DPanel extends JPanel implements Runnable{
	
	private static final long serialVersionUID = -1722759515903792828L;
	private int width;
	private int height;
	private Color bkColor;
	
	private final long refreshWaitMillis = 10;
	
	private GameStatus status;
	private Ship userShip;
	private Controller2D controller2d;
	private ScoreDrawer scoreDrawer;
	
	private BufferedImage shipSprite;
	private BufferedImage bulletSprite;
	private BufferedImage galaxyBackground;
	
	private Assets assets;	

	public SinglePlayer2DPanel( SinglePlayer singlePlayer, ScoreDrawer scoreDrawer ) {
		super();
		this.width = ViewParam.WIDTH;
		this.height = ViewParam.HEIGTH;
		this.bkColor = ViewParam.BACKGROUND_COLOR;
		
		this.status = singlePlayer.getStatus();
		this.scoreDrawer = scoreDrawer;
		userShip = status.getUserShip();
		controller2d = new Controller2D(userShip);
		
		assets = Assets.getsLoader();
		
		initSprites();
		
		initPanel();
		
		this.addKeyListener( controller2d );
	}
	
	private void initSprites(){
		shipSprite = assets.getAsset(Assets.SHIP_2D);
		bulletSprite = assets.getAsset(Assets.BULLET_AVIABLE_INDICATOR);
	}
	
	private void initPanel(){
		setFocusable(true);
		setPreferredSize(new Dimension(width, height));
		setBackground(bkColor);
	}
	
	public void setScoreDrawer(ScoreDrawer scoreDrawer) {
		this.scoreDrawer = scoreDrawer;
	}
	
	private void draw(Graphics g){
		drawBackground(g);
		drawMobs(g);
		drawShip(g);
		drawGUI(g);
	}
	
	private void drawBackground( Graphics g){
		g.drawImage(galaxyBackground, 0, 0, null);
	}
	
	private void drawMobs(Graphics g){
		Iterator<String> IDs = status.getMobsMapSet().iterator();
		Coordinate coo;
		String ID;
		Mob mob;
		while( IDs.hasNext() ){
			ID = IDs.next();
			mob = status.getMob( ID );
			coo = mob.getCoordinate();
			g.drawImage( assets.getAsset( mob.spriteFamily() ), coo.getX(), coo.getY(), null);
		}
	}
	
	private void drawShip(Graphics g){
		Coordinate coordinate = userShip.getCoordinate();
		//lascio il cast nel caso si cambino poi i tipo alle coordinate...
		g.drawImage(shipSprite, (int)coordinate.getX(), (int)coordinate.getY(), null);
	}
	
	private void drawGUI(Graphics g){
		drawBullets(g);
		
		
		//TODO
		long testScore = 999999L;
		scoreDrawer.drawScore(g, testScore);
	}
	
	private void drawBullets(Graphics g){
		int gapH = 20;
		int gapV = 4;
		int bulletX = 10;
		int bulletY = height-bulletSprite.getHeight()-gapH;
		for (int i = 0; i < userShip.getAviavailableAmmo() ; i++) {
			g.drawImage( bulletSprite, bulletX + gapH*i , bulletY + gapV*i, null );
		}
	}
	
	
	
	@Override
	public void run() {
		while(true){
			repaint();
			try {
				Thread.sleep(refreshWaitMillis);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		draw(g);
	}
}
